package avicit.bdp.dcs.csv;

import avicit.bdp.core.poi.util.csv.ExcelUtils;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TxtCsvTest {

  private static BufferedWriter bufferedWriter = null;
  private static BufferedReader bufferedReader = null;

  public static void main(String[] args) {
    //        readAndWrite();
    ff();
    //        generateExcel();
  }

  public static void readAndWrite() {
    try {
      bufferedReader =
          new BufferedReader(
              new InputStreamReader(
                  new FileInputStream("E:\\2022\\大文件\\RepWiwj20181209_2000W.csv"), "UTF-8"));

      bufferedWriter =
          new BufferedWriter(
              new OutputStreamWriter(
                  new FileOutputStream("E:\\2022\\house_info_1.txt", true), "UTF-8"));

      String str;
      long i = 0;
      while (null != (str = bufferedReader.readLine())) {
        if (i != 0) {
          //                    System.out.println(str);
          bufferedWriter.write(str + "\n");
          bufferedWriter.flush();
        }

        //                if(i >= 20596259 && i < 20596300){
        //                    System.out.println(str);
        //                }

        if (i >= 2) {
          break;
        }

        i++;
      }
      bufferedReader.close();

      bufferedWriter.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static void readAndWrite1() {
    try {
      bufferedReader =
          new BufferedReader(
              new InputStreamReader(new FileInputStream("E:\\2022\\house_info_20w.txt"), "UTF-8"));

      bufferedWriter =
          new BufferedWriter(
              new OutputStreamWriter(
                  new FileOutputStream("E:\\2022\\house_info_20w1.csv", true), "UTF-8"));

      String str;
      long i = 0;
      while (null != (str = bufferedReader.readLine())) {
        //                if(i != 0){
        //                    System.out.println(str);
        bufferedWriter.write(str + "\n");
        bufferedWriter.flush();
        //                }

        //                if(i >= 20596259 && i < 20596300){
        //                    System.out.println(str);
        //                }

        if (i >= 2) {
          break;
        }
        i++;
      }
      bufferedReader.close();

      bufferedWriter.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static List<Map<String, Object>> read() {
    List<Map<String, Object>> dataList = new ArrayList<>();

    try {
      bufferedReader =
          new BufferedReader(
              new InputStreamReader(
                  new FileInputStream("E:\\2022\\大文件\\RepWiwj20181209_2000W.csv"), "UTF-8"));

      String str;
      long i = 0;
      while (null != (str = bufferedReader.readLine())) {
        if (i != 0) {
          Map<String, Object> map = new HashMap<>();

          String[] arr = str.split(",");
          map.put("sxh2", arr[0]);
          map.put("fwjj", arr[1]);
          map.put("tj", arr[2]);
          map.put("xq", arr[3]);
          map.put("fx", arr[4]);
          map.put("mj", arr[5]);
          map.put("cx", arr[6]);
          map.put("jz", arr[7]);
          map.put("dt", arr[8]);
          map.put("lc", arr[9]);
          map.put("jzjg", arr[10]);
          map.put("jq", arr[11]);
          map.put("gzrs", arr[12]);
          map.put("kfcs", arr[13]);
          map.put("ditie", arr[14]);
          map.put("sf", arr[15]);
          map.put("kfsj", arr[16]);
          map.put("fwzj", arr[17]);
          map.put("mjdj", arr[18]);

          dataList.add(map);
        }

        if (i >= 200000) {
          break;
        }

        i++;
      }
      bufferedReader.close();
    } catch (IOException e) {
      e.printStackTrace();
    }

    return dataList;
  }

  public static void generateExcel() {
    ExcelUtils excelUtils = new ExcelUtils();

    try {
      List<Map<String, Object>> dataList = read();

      List<ExcelExportEntity> entityList = new ArrayList<>();
      entityList.add(new ExcelExportEntity("sxh2", "sxh2", 30));
      entityList.add(new ExcelExportEntity("fwjj", "fwjj", 30));
      entityList.add(new ExcelExportEntity("tj", "tj", 30));
      entityList.add(new ExcelExportEntity("xq", "xq", 30));
      entityList.add(new ExcelExportEntity("fx", "fx", 15));
      entityList.add(new ExcelExportEntity("mj", "mj", 15));
      entityList.add(new ExcelExportEntity("cx", "cx", 15));
      entityList.add(new ExcelExportEntity("jz", "jz", 15));
      entityList.add(new ExcelExportEntity("dt", "dt", 15));
      entityList.add(new ExcelExportEntity("lc", "lc", 15));
      entityList.add(new ExcelExportEntity("jzjg", "jzjg", 15));
      entityList.add(new ExcelExportEntity("jq", "jq", 20));
      entityList.add(new ExcelExportEntity("gzrs", "gzrs", 20));
      entityList.add(new ExcelExportEntity("kfcs", "kfcs", 30));
      entityList.add(new ExcelExportEntity("ditie", "ditie", 20));
      entityList.add(new ExcelExportEntity("sf", "sf", 20));
      entityList.add(new ExcelExportEntity("kfsj", "kfsj", 20));
      entityList.add(new ExcelExportEntity("fwzj", "fwzj", 20));
      entityList.add(new ExcelExportEntity("mjdj", "mjdj", 20));

      String title = "house_info_20w";
      ExportParams exportParams = new ExportParams(null, title);
      exportParams.setType(ExcelType.XSSF);

      Workbook workbook = ExcelExportUtil.exportExcel(exportParams, entityList, dataList);

      excelUtils.generateExcel(title, workbook, "E:\\2022\\");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static void ff() {
    try {
      String suffix = "xlsx";
      String newFilePath = "E:\\2022\\house_info_20w_1.csv";
      FileInputStream fis = new FileInputStream("E:\\2022\\house_info_20w.xlsx");

      //            Workbook wb = new HSSFWorkbook(fis);
      //            Workbook wb = new XSSFWorkbook(fis);

      System.out.println("123");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
